import { ref, computed , onMounted } from 'vue'
import { defineStore } from 'pinia'
import { getLocal } from '@/apis/place'
import { getWeather ,getNowWeather} from '@/apis/weather'
// 定义counter空间
export const useLocalWeatherStore = defineStore('localWeather', () => {
  // 定义city变量
  const LocalCity = ref('')
  const LocalWeather = ref('')
  const nowWeather=ref('')
  // 定义cityList变量

  // 获取本地存储的城市列表
  const getLocalCity = async () => {
    const res = await getLocal()
    LocalCity.value = res.data
    //console.log(LocalCity.value)
  }
  const getNowWeatherData = async (city) => {
    //console.log(city)
    const res = await getNowWeather(city)
     return res.data
  
  }
  const getWeatherData = async (city) => {
    const res = await getWeather(city)
    return res.data
  }


  onMounted( () => {
     getLocalCity().then(() => {
      getNowWeatherData(LocalCity.value.adcode).then(res =>{
        nowWeather.value=res.lives[0]
        //console.log(nowWeather.value)
      })
      getWeatherData(LocalCity.value.adcode).then(res =>{
        LocalWeather.value=res.forecasts[0].casts
        console.log(LocalWeather.value)
      })
     
     })
    
  })
   return{ LocalCity,nowWeather,LocalWeather}
})